package com.jk.lesson11;

import com.jk.lesson7.lesson7_1;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Created by Auser on 2017/3/15.
 */
public class lesson_11_1 {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newCachedThreadPool();
        Future<Double> future = executor.submit(new Callable<Double>(){
            @Override
            public Double call() throws Exception {
                return doSomethingElse();
            }
        });
        doSomeLongComputation();

        try {
            Double result = future.get(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            // 计算抛出异常
            e.printStackTrace();
        } catch (ExecutionException e) {
            // 当前线程在等待过程中被中断
            e.printStackTrace();
        } catch (TimeoutException e) {
            // 在Future对象完成之前超过已过期
            e.printStackTrace();
        }
    }

    private static Double doSomeLongComputation(){
        System.out.println("SequentialRange sum done in : " +
                lesson7_1.measureSumPerf(lesson7_1::sequentialRangedSum, 1_000_000_000) + " ms");
        return new Double("123.1");
    }

    private static Double doSomethingElse(){
        System.out.println("ParallelRange sum done in : " +
                lesson7_1.measureSumPerf(lesson7_1::parallelRangedSum, 100_000_000) + " ms");
        return new Double("123.1");
    }
}
